/**
 * 海康用户配置compHkUser.js
 * 版本号： v1.0.0
 */
$(function(){

    //公司下拉树
    $('#companyTree').tree({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        lines:true,
        /*onClick:function(node){
            getHkUser(node.id);
        },*/
        onLoadSuccess:function(node, data){
            var root = $(this).tree("find", data[0].id);
            $(this).tree("select", root.target);
        },
        onSelect:function(node){
            getHkUser(node.id);
        },
        onContextMenu: function(e, node){
            e.preventDefault();
            // 查找节点
            $('#companyTree').tree('select', node.target);
            // 显示快捷菜单
            $('#userAllot').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    });

});

/**
 * 根据公司获取已分配的用户
 * @param compId
 */
function getHkUser(companyId) {
    $("#compHkUser_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        border: false,
        url: CONTEXT_PATH + '/compHkUser/compHkUserList.do?companyId='+companyId,
        idField: 'chkId',
        frozenColumns: [[
            {field: 'chkId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'loginAccount', title: '登录名', width: 150,fixed:true},
            ]
        ]
    });
    $('#compHkUser_dataList').datagrid('clearSelections');
    //定义可操作按钮
    $("#compHkUser_dataList").datagrid({
        border: false,
        fit: true,
        toolbar: [
            {
                id:btnOptions[i].value.meId,
                text:btnOptions[i].value.name,
                iconCls:btnOptions[i].value.iconCls,
                handler: function () {
                    hkUser_del();
                }
            }
        ]
    });
}

/**
 * 移除用户
 */
function hkUser_del(){
    var row = $("#compHkUser_dataList").datagrid("getSelections");
    if (row.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要删除的用户", timeout: 3000, showType: "slide" });
        return;
    };
    var chkId = "";
    for(var i=0;i<row.length;i++){
        chkId += row[i].chkId +",";
    }

    var companyId = $("#companyTree").tree('getSelected').id;
    $.messager.confirm('删除提示', '您确定要删除选中的用户吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/compHkUser/delCompHkUser.do?chkId=" + chkId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#compHkUser_dataList").datagrid("reload",{companyId:companyId});
                    $('#compHkUser_dataList').datagrid('clearSelections');

                }
            })
        }
    });
}

/**
 * 为选中的公司分配人员
 */
function append(){
    $("#compAllotUser_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        border: false,
        url: CONTEXT_PATH + '/compHkUser/getNotAllotUser.do',
        idField: 'userUuid',
        frozenColumns: [[
            {field: 'userUuid', checkbox: true}
        ]],
        columns: [
            [
                {field: 'loginAccount', title: '登录名', width: 150}
            ]
        ]
    });
    $('#compAllotUser_dataList').datagrid('clearSelections');

    $("#compAllotUser_dialog").dialog({
        title: '人员配置',
        width: 400,
        modal: true,
        closed: true,
        height: 400,
        iconCls: "icon-custom-search",
        buttons: [{
            text: "保存",
            iconCls: "icon-role-state-ok",
            handler: function () {
                compHkUser_allot();
            }
        }, {
            text: "取消",
            iconCls: "icon-role-state-no",
            handler: function () {
                $('#compAllotUser_dialog').dialog('close');
            }
        }]
    });
    $("#compAllotUser_dialog").dialog("move", {left: 450, top: 100});
    $("#compAllotUser_dialog").dialog('open');
}

/**
 * 执行保存
 */
function compHkUser_allot(){
    var companyId = $("#companyTree").tree('getSelected').id;
    var row = $("#compAllotUser_dataList").datagrid("getSelections");
    if(companyId == null || companyId == ""){
        $.messager.show({title: '操作提示', msg: "请选择公司", timeout: 3000, showType: "slide"});
        return;
    }
    if(row.length == 0){
        $.messager.show({title: '操作提示', msg: "请选择用户", timeout: 3000, showType: "slide"});
        return;
    }
    var userUuids = "";
    for(var i=0;i<row.length;i++){
        userUuids += row[i].userUuid +",";
    }
    $.ajax({
        url: CONTEXT_PATH + "/compHkUser/save.do",
        type: "post",
        data: {companyId:companyId,userUuid:userUuids},
        success: function (data) {
            messager(data);
            //刷新用户
            $("#compHkUser_dataList").datagrid("reload",{companyId:companyId});
            $('#compAllotUser_dialog').dialog('close');
        }
    })

}
